package org.duang.kit;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ConcurrentLinkedQueue;

import org.duang.common.exceptios.ServiceException;
import org.duang.config.Config;
import org.duang.email.EmailClient;
import org.duang.email.MailSmtpConfig;
import org.duang.logs.Logger;

/**
 * EmailKit 邮件发送工具类
 * 
 * @author laotang
 */
public class EmailKit {

	private static Logger logger = LogKit.getLogger(EmailKit.class);
//	private static java.util.concurrent.locks.ReentrantLock lock = new java.util.concurrent.locks.ReentrantLock();
//	private static ConcurrentLinkedQueue<EmailKit> queue = new ConcurrentLinkedQueue<EmailKit>();
	private static EmailKit emailKit;
	private String account;
	private String password;
	private String subject;
	private String content;
	private MailSmtpConfig smtpConfig;
	private Set<String> receivers = new HashSet<String>();
	private boolean isHtmlType = false;

	private EmailKit() {

	}

	private EmailKit(String account, String password, String subject, String content, Set<String> receivers,
			boolean isHtmlType) {
		this.account = account;
		this.password = password;
		this.subject = subject;
		this.content = content;
		this.receivers = receivers;
		this.isHtmlType = isHtmlType;
	}

	public static EmailKit duang() {
//		synchronized (lock) {
//			if (null == emailKit) {
				emailKit = new EmailKit();
//			}
//		}
//		emailKit.clear();
		return emailKit;
	}

	public EmailKit from(String from) {
		account = from;
		return emailKit;
	}

	public EmailKit pwd(String password) {
		this.password = password;
		return emailKit;
	}

	public EmailKit to(String to) {
		receivers.add(to);
		return this;
	}

	public EmailKit to(Set<String> tos) {
		receivers.addAll(tos);
		return this;
	}

	public EmailKit subject(String subject) {
		this.subject = subject;
		return this;
	}

	public EmailKit html(String htmlString) {
		this.content = htmlString;
		isHtmlType = true;
		return this;
	}

	public EmailKit text(String textString) {
		this.content = textString;
		return emailKit;
	}

	/**
	 * smtp config，默认的是qq，如有需要，自行设置所需的值
	 * 
	 * @param config
	 * @return
	 */
	public EmailKit config(MailSmtpConfig config) {
		smtpConfig = config;
		return emailKit;
	}

	private void checkValue() {
		if (ToolsKit.isNotEmpty(smtpConfig))
			EmailClient.duang().setSmtpConfig(smtpConfig);
		if (ToolsKit.isEmpty(account))
			account = Config.getEmailUser();
		if (ToolsKit.isEmpty(password))
			password = Config.getEmailPwd();
	}

	private void clear() {
		account = "";
		password = "";
		subject = "";
		content = "";
		smtpConfig = null;
		isHtmlType = false;
		receivers.clear();
	}

	public boolean send() {
		checkValue();
		try {
			if (isHtmlType) {
				EmailClient.duang().sendHtmlMail(account, password, subject, content, receivers);
			} else {
//				System.out.println("receivers3333333333333333: " +receivers.toString());
				EmailClient.duang().sendTextMail(account, password, subject, content, receivers);
			}
//			clear();
			return true;
		} catch (Exception e) {
			logger.print(e.getMessage(), e);
			throw new ServiceException(e.getMessage(), e);
		}
	}

	public synchronized void asynSend() {		
		
//		EmailKit mailKit = new EmailKit(account, password, subject, content, receivers, isHtmlType);
//		System.out.println(mailKit.hashCode()+"              receivers111111111111: " +mailKit.receivers.toString());
//		queue.add(mailKit);
		ThreadPoolKit.execute(new Thread() {
			public void run() {
				try {
//					EmailKit kit = queue.poll();
//					System.out.println(kit.hashCode()+"                    receivers2222222222 : " + kit.receivers.toString());
//					queue.poll().send();
					send();
				} catch (Exception e) {
					logger.print(e.getMessage(), e);
				}
			}
		});
	}
}
